package com.example.arithmeticleetcode.leetcode.September;

/**
 * @program: arithmetic-leetcode
 * @description:
 * @author: FangZhen
 * @create: 2020-09-15 13:44
 **/
public class Demo12 {

    public static void main(String[] args) {
        int[][] mat = {{1, 0, 0}, {0, 0, 1}, {1, 0, 0}};
        System.out.println(numSpecial2(mat));
        int[][] mat1 = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
        System.out.println(numSpecial2(mat1));
    }

    public static int numSpecial(int[][] mat) {
        int count = 0;
        for (int i = 0, len = mat.length; i < len; i++) {
            for (int j = 0, jLen = mat[i].length; j < jLen; j++) {
                if (mat[i][j] == 1 && addRowColume(mat, i, j) == 1) {
                    count++;
                }
            }
        }
        return count;

    }


    public static int addRowColume(int[][] mat, int row, int colume) {
        int result = 0;
        for (int i : mat[row]) {
            result += i;
        }

        for (int i = 0, len = mat.length; i < len; i++) {
            if (i != row) {
                result += mat[i][colume];
            }
        }
        return result;
    }


    public static int numSpecial2(int[][] mat) {
        int count = 0;
        for (int i = 0, len = mat.length; i < len; i++) {
            for (int j = 0, jLen = mat[i].length; j < jLen; j++) {
                if (mat[i][j] == 1 && addRowColume2(mat, i, j)) {
                    count++;
                }
            }
        }
        return count;

    }


    public static boolean addRowColume2(int[][] mat, int row, int colume) {
        int result = 0;
        for (int i : mat[row]) {
            result += i;
            if (result >= 2) {
                return false;
            }
        }

        for (int i = 0, len = mat.length; i < len; i++) {
            if (i != row) {
                result += mat[i][colume];
                if (result >= 2) {
                    return false;
                }
            }
        }
        return true;
    }
}
